<?php

namespace App\Admin\Controllers;

use App\Admin\Filters\TimestampBetween;
use App\Models\Users;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class UserController extends BaseController
{
    const   INVITE_STATUS = [
        'on'  => ['value' => 0, 'text' => '无', 'color' => 'default'],
        'off' => ['value' => 1, 'text' => '有', 'color' => 'danger'],
    ];
    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('用户列表')
            ->description(' ')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('用户详情')
            ->description(' ')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑用户')
            ->description(' ')
            ->body($this->form($id)->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新增会员')
            ->description(' ')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Users());
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->column(1 / 2, function ($filter) {
                $filter->equal('id', '用户ID');

                $filter->equal('mobile', '手机号');
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->like('nickname', '昵称');
                $filter->use(new TimestampBetween('created_at', __('created_at')))->datetime();
            });
        });
//
        $grid->model()->orderBy('created_at', 'desc');
        $grid->column('id', '会员ID')->sortable();
        $grid->column('nickname', '会员昵称');
        $grid->column('mobile', '手机号');


        $grid->column('created_at', __('created_at'));

        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $actions->disableView();

        });
//
        $grid->disableCreateButton();
        $grid->disableExport();
        $grid->tools(function ($tools) {
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });
        $grid->expandFilter();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Users::findOrFail($id));

        $show->panel()
            ->tools(function ($tools) {
                $tools->disableDelete();
            });
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($id = 0)
    {
        $form = new Form(new Users);


        $form->radio('status', '会员状态')->options([0 => '作废', 1 => '正常']);
        $form->image('avatar','头像')->uniqueName();

        $form->tools(function (Form\Tools $tools) {
            // 去掉`删除`按钮
            $tools->disableDelete();
        });

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
            $footer->disableReset();
        });
        return $form;
    }

}
